package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.activity.dragonboatrace.DragonBoatRaceMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class BusyFarmingRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = BusyFarmingMgr.getActivityInfo();
        if (activityInfo != null) {
            for (Integer itemId : BusyFarmingMgr.getBusyFarmingSeedGoods()) {
                long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
                List<String> paramList = new ArrayList<>();
                paramList.add(activityInfo.getActivityName());
                paramList.add(String.valueOf(BusyFarmingMgr.BUSY_FARMING_FRUIT_SCORE_ID));
                paramList.add(String.valueOf(BusyFarmingMgr.BUSY_FARMING_RECOVERY_DISCOUNT));
                ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 1, recycleTime, paramList);
            }

            int farmingManureId = BusyFarmingMgr.BUSY_FARMING_MANURE_ID;
            long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
            List<String> paramList = new ArrayList<>();
            paramList.add(activityInfo.getActivityName());
            paramList.add(String.valueOf(BusyFarmingMgr.BUSY_FARMING_FRUIT_SCORE_ID));
            paramList.add(String.valueOf(BusyFarmingMgr.BUSY_FARMING_RECOVERY_DISCOUNT));
            ActivityItemRecycleMgr.addRecycleItemData(farmingManureId, activityType, 2, recycleTime, paramList);
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = BusyFarmingMgr.getActivityInfo();
        return ActivityMgr.activityInTime(activityInfo);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.BusyFarmingActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        if (recycleType == 1) {
            removeSeedItem(userId, data);
        } else if (recycleType == 2) {
            removeSeedItem(userId, data);
        }
    }

    private void removeFarmingManure(long userId, ActivityItemRecycleData data) {

    }

    private void removeSeedItem(long userId, ActivityItemRecycleData data) {
        String language = UserMgr.getUserInfo(userId).getLanguage();
        List<String> paramList = data.getParamList();
        int i = 0;
        Object[] paramArr = new Object[12];
        String activityName = ServerLanguageMgr.getContent(paramList.get(0), language);
        paramArr[i++] = activityName;
        int removeItemId = Integer.parseInt(paramList.get(1));
        GoodsInfo scoreGoodsInfo = GoodsMgr.getGoodsById(removeItemId);
        if (scoreGoodsInfo == null) {
            log.error("remove  activity {} goods to score error,goods no found,goodsId:{}", activityName, removeItemId);
            return;
        }
        Property reward = new Property();
        Property consume = new Property();
        int removeGoodsId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, removeGoodsId);
        long count = 0;
        if (null != userBagItem && userBagItem.getNum() > 0) {
            count = userBagItem.getNum();
        }
        if (count > 0) {
            consume.addProperty(removeGoodsId, BigInteger.valueOf(count));
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);

        int discount = Integer.parseInt(paramList.get(2));
        if (goodsInfo != null) {
            long score = calcScore(removeGoodsId, (int) count, discount, data.getRecycleType());
            reward.addProperty(removeItemId, BigInteger.valueOf(score));
            String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
            paramArr[i++] = goodsName;
            paramArr[i++] = count;
        }

        if (!consume.isNothing()) {
            if (removeItem(userBagItem)) {
                String rewardStr = PropertyHelper.parsePropertyToString(reward);
                String scoreName = ServerLanguageMgr.getContent(scoreGoodsInfo.getGoodsName(), language);
                paramArr[i++] = scoreName;
                String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                        language), activityName);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                        language), paramArr);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
            }
        }
    }

    private long calcScore(int removeGoodsId, int count, int discount, int recycleType) {
        if (recycleType == 1) {
            return BusyFarmingMgr.getSeedMaturityFruitScore(removeGoodsId, discount, count);
        } else if (recycleType == 2) {
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
            return BigDecimal.valueOf(goodsInfo.getParamList().get(1).longValue()).multiply(BigDecimal.valueOf(count))
                    .multiply(BigDecimal.valueOf(discount)).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
        }
        return 0;
    }
}
